package cn.itcast.bos.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.utils.BaseAction;
public class CourierAction extends BaseAction<Courier> {
	@Autowired
	private CourierService courierService;
	
	@Action(value="courier_save",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String save(){
		courierService.save(model);
		return SUCCESS;
	}
	
	@Action(value="courier_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		
		Pageable pageable =  new PageRequest(page-1, rows);
		//构造条件查询
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(model.getCourierNum())){
					//进行快递员工号查询
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class),model.getCourierNum());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					//进行快递员所属公司查询
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(model.getType())){
					//进行快递员类型查询
					Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
					list.add(p3);
				}
				//多表查询(查询当前对象关联对象对应的数据表)
				//使用Courier(root)关联standard
				Join<Object, Object> standardRoot = root.join("standard",JoinType.INNER);
				//standardRoot代表standard那张表中的字段属性部分，根据standardRoot.get("name")判断
				//，因为courier表中也有name属性
				if(model.getStandard()!=null && 
						StringUtils.isNotBlank(model.getStandard().getName())){
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class), "%"+model.getStandard().getName()+"%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Courier> page = courierService.findAll(specification,pageable);
		
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	//属性注入要作废或还原的快递员id拼接成的字符串
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	//作废快递员
	@Action(value="courier_deltag",
			results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String deltag(){
		String[] arrays = ids.split("-");
		courierService.deltag(arrays);
		return SUCCESS;
	}
	//还原作废的快递员
	@Action(value="courier_restore",
			results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String restore(){
		String[] arrays = ids.split("-");
		courierService.restore(arrays);
		return SUCCESS;
	}
	
	//查询未关联到定区的快递员
	@Action(value="courier_findNoAssociationFixedAreaCouriers"
			,results={@Result(name="success",type="json")})
	public String findNoAssociationFixedArea(){
		List<Courier> couriers = courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
}
